﻿using System;
using System.Collections.Generic; 
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Runda.EShop.Common.Component
{
    [DefaultProperty("Data")]
    [ValidationProperty("Data")]
    [ControlValueProperty("Data")]
    [ToolboxData("<{0}:OrderProcess runat=server></{0}:OrderProcess>")]
    public class OrderProcess : WebControl, IPostBackDataHandler
    {
          #region Properties 

        /// <summary>
        /// 当前订单ID
        /// </summary>
        public DataTable OrderStatusTable
        {
            get;
            set;
        }

        /// <summary>
        /// 数据
        /// </summary>
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Data
        {
            get
            {
                return ViewState["Data"] == null ? string.Empty : ViewState["Data"].ToString();
            }
            set
            {

                ViewState["Data"] = value;
            }
        }
        #endregion

        #region Constructor

        /// <summary>
        /// 无参构造函数
        /// </summary>
        public OrderProcess()
            : base(HtmlTextWriterTag.Div)
        {
        }

        #endregion

        #region Events

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        /// <summary>
        /// 渲染内容
        /// </summary>
        /// <param name="writer"></param>
        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.WriteLine("<div id=\"process\" class=\"section3\">");

            string strFormat1 = "<div class=\"{0}\"><ul><li class=\"tx1\">&nbsp;</li><li class=\"tx2\">{1}</li><li class=\"tx3\">{2}</li></ul></div>";
            string strFormat2 = "<div class=\"{0}\"><ul><li class=\"tx1\">&nbsp;</li></ul></div>";

            StringBuilder sb = new StringBuilder();
            bool flag = true;
            for (int i = 0; i < OrderStatusTable.Rows.Count; i++)
            {
                if (i == OrderStatusTable.Rows.Count - 1)
                {
                    if (Convert.ToInt32(OrderStatusTable.Rows[i]["ProcessStatus"].ToString()) == 0)
                    {
                        sb.AppendLine(string.Format(strFormat1, "node wait", OrderStatusTable.Rows[i]["StatusName"].ToString(), OrderStatusTable.Rows[i]["CreatedAt"].ToString()));
                    }
                    else
                    {
                        sb.AppendLine(string.Format(strFormat1, "node ready", OrderStatusTable.Rows[i]["StatusName"].ToString(), OrderStatusTable.Rows[i]["CreatedAt"].ToString()));
                    }
                }
                else
                {
                    if (Convert.ToInt32(OrderStatusTable.Rows[i]["ProcessStatus"].ToString()) == 0 && flag)
                    {
                        sb.AppendLine(string.Format(strFormat1, "node singular", OrderStatusTable.Rows[i]["StatusName"].ToString(), OrderStatusTable.Rows[i]["CreatedAt"].ToString()));
                        sb.AppendLine(string.Format(strFormat2, "proce wait"));
                        flag = false;
                    }
                    else
                    {
                        if (Convert.ToInt32(OrderStatusTable.Rows[i]["ProcessStatus"].ToString()) == 0)
                        {
                            sb.AppendLine(string.Format(strFormat1, "node wait", OrderStatusTable.Rows[i]["StatusName"].ToString(), OrderStatusTable.Rows[i]["CreatedAt"].ToString()));
                            sb.AppendLine(string.Format(strFormat2, "proce wait"));
                        }
                        else
                        {
                            sb.AppendLine(string.Format(strFormat1, "node ready", OrderStatusTable.Rows[i]["StatusName"].ToString(), OrderStatusTable.Rows[i]["CreatedAt"].ToString()));
                            sb.AppendLine(string.Format(strFormat2, "proce ready"));
                        }
                    }
                }

            }
            writer.WriteLine(@"</div>");

          
            base.RenderContents(writer);
        }

        #endregion

        #region Methods
           
         

        #endregion

        #region Interface member

        /// <summary>
        /// 加载回发数据
        /// </summary>
        /// <param name="postDataKey"></param>
        /// <param name="postCollection"></param>
        /// <returns></returns>
        public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            //this.Data = postCollection[postDataKey];
            return false;
        }

        /// <summary>
        /// 触发回发数据变更事件
        /// </summary>
        public void RaisePostDataChangedEvent()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
